import TableLyout from '@/components/commonPublic/tableLyoutDetail.vue'
import TableData from "@/components/commonPublic/el-table"; //主页面列表
import CustomDialog from "@/components/BehaViorManage/annualCreditReviewText/index.vue";
import score from "@/components/BehaViorManage/annualCreditReviewText/score.vue";
import {
    deleteCreditEvaluationProcess, //删除
    getCreditEvaluationProcessLists, //列表页接口
    submitCreditEvaluationProcess, //监理提交
    submitCreditEvaluationProcessByGroup, //集团提交
    submitCreditEvaluationProcessByOwner, //业主提交
} from "@/api/request.js";
import publicForm from "@/components/QualityGridManage/fromCom.vue"; //弹框form
import tree from "@/components/QualityGridManage/tree"; //左侧机构树
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    jlButton
} from "@/utils/config"; //配置项

export default {
    name: "annualCreditReviewText",
    data() {
        return {
            name: "annualCreditReviewText",
            btnObj: [], //按钮
            isLock: false,
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            radio: "待办",
            isShow1: false,
            formData: {
                fId: "", // 工程信息fid  有值时表示修改  其余表示新增
                fCompanyid: "", // 施工单位id
                fCompanyname: "", // 施工单位名称
                fProjectid: "", // 项目id
                fProjectname: "", // 项目名称
                fEngineeringname: "", // 工程名称
                fEngineeringlocation: "", // 工程地点
                fEngineeringinfo: "", // 工程信息简述
                fDesignfile: "", // 设计资料文件id
                fPiercount: "", // 墩台数量
                fFoundationcount: "", // 基桩数量
                fRegistrantid: "", // 登记人id
                fRegistrantname: "", // 登记人名称
                fRegistdates: "", // 登记时间
                // 当前用户数据
                userId: "", // 当前登陆用户id
                userName: "", // 当前登陆用户名称
                recordfiles: [], //删除文件id集合--修改时传
            },
            // 列表
            getList: {
                fCompanyid: "", //分子公司或建设项目id
                keyType: "", //计划类型
                fStatus: "", //完成情况(待审核,执行中,已打回,已完成)
                companyid: "", //项目部id
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
                userId: "", // 查询待办或已办时传 列表页查询不传
                modelType: "0" // 0-待办 1-已办 传2 表示列表页查询
            },
            isShow: false, //是否显示弹框
            status: null, //按钮状态1新增 3编辑
            statusNum: null,
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            fFileName: "",
            pdfUrl: "",
            fPhotofilelist: [], //检测记录表文件集合
            samllTable: [], //检测任务信息列表
            picsfile: [], //现场照片
            exceptionfiles: [], //异常资料
            checkfiles: [], //检测资料资料
            handlerfiles: [], //处理照片
            lrCheckPiertask: {},
            checkFile: [], //本次检测附件资料信息
            lrCheckPierfoundation: {}, // 基桩检测表格信息集合
            activeIndex: "1",
            dis: true,
            fEquipmentname: [], //已选主要仪器设备
            dialogText: "",

            equipmentDetailId: null, //列表页id
            equipmentDetailIds: null, //检测任务信息列表页id
            sample: "",
            fId: null, //当前数据fId
            loading: false,
            dialogTableVisible: false,
            formLabelWidth: "150px",
            tableData: [],
            companyId: null,
            loadingDialog: false,
            currentRowId: "",
        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        tree,
        elBreadcrumb,
        CustomDialog,
        score
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
        this.getList.userId = this.userInfo.userId
        this.getList.fCompanyid = this.userInfo.companyId
        this.yj_searchForm(this.getList);
        this.table_config.thead[6]["callback"] = function (rowData) {
            let data = jlButton[rowData.jlButton];
            if (data) {
                return data.label;
            }
        };
        this.table_config.thead[6]["specialClass"] = function (rowData) {
            let data = jlButton[rowData.jlButton];
            if (data) {
                return data.className;
            }
        };
        this.table_config.thead[11]["callback"] = function (rowData) {
            let data = jlButton[rowData.yzButton];
            if (data) {
                return data.label;
            }
        };
        this.table_config.thead[11]["specialClass"] = function (rowData) {
            let data = jlButton[rowData.yzButton];
            if (data) {
                return data.className;
            }
        };
        this.table_config.operation.cols = [{
            prop: "fStatus",
            type: "primary",
            handleRow: this.handleRow,
        }, ];
        this.table_config.operation["specialClass"] = function () {
            return "#409eff";
        };
        this.btnRole(this.name)
    },
    methods: {
        refreshList() { //刷新列表
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        closeDialog() {
            this.isShow = false;
        },
        closeDialog1() {
            this.isShow1 = false;
        },
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    btnList.map(item => {
                        item['disabled'] = false
                    })
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = formJs.btnObj
            }
            // this.btnObj = formJs.btnObj
        },
        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },
        // 重置按钮
        modelType(e) {
            this.fId = "";
            if (e == "待办") {
                this.getList.modelType = "0";
            } else {
                this.getList.modelType = "1";
            }
            this.yj_searchForm(this.getList);
        },
        // 新增/编辑
        yj_edit(num) {
            if (num == 1) { //新增
                this.status = [true, false, false]
                // 新增
                if (this.userInfo.companyNature == '总监办') {
                    this.editText();
                } else {
                    this.$message.warning("该用户无法新增");
                }
            } else if (num == 2) { //编辑
                if (this.dataFid != null) {
                    // 新增、编辑、删除、提交
                    if (this.currentRow.fStatus == '业主审核中' || this.currentRow.fStatus == '审核未通过' || this.currentRow.fStatus == '集团审批中') {
                        this.$message.warning("您无此权限");
                    } else {
                        this.status = [true, false, false, false]
                        // 编辑
                        this.editText();
                    }
                } else {
                    this.$message.warning("请先选择需要编辑的数据");
                }
            } else if (num == 3) { //提交


                if (this.dataFid != null) {
                    this.yj_submit(0)
                } else {
                    this.$message.warning("请先选择需要提交的数据");
                }
            } else if (num == 4) {
                if (this.dataFid != null) {
                    if (this.currentRow.fStatus == '监理自评中' || this.currentRow.fStatuss == '审核未通过' || this.currentRow.fStatus == '已驳回') {
                        this.$message.warning("您无此权限");
                    } else {
                        var status1 = this.currentRow //当前数据状态
                        if (status1 != null && status1.fStatus == '业主审核中') {
                            this.status = [true, true, false]
                        } else if (status1 != null && status1.fStatus == '集团审批中') {
                            this.status = [true, true, true]
                        }
                        // 编辑
                        this.editText();
                    }
                } else {
                    this.$message.warning("请先选择需要审核的数据");
                }
            } else if (num == 5) {
                if (this.dataFid != null) {
                    // if (this.currentRow.fStatus == '业主审核中' ||this.currentRow.fStatuss == '待审核' || this.currentRow.fStatuss == '执行中' || this.currentRow.fStatuss == '审核中' || this.currentRow.fStatuss == '已打回') {
                    //     this.$message.warning("您无此权限");
                    // } else {
                    //     this.yj_delete()
                    // }
                    if (this.currentRow.fStatus == '监理自评中') {
                        
                        this.yj_delete()
                    } else {
                        this.$message.warning("您无此权限");
                    }
                } else {
                    this.$message.warning("请先选择需要删除的数据");
                }
            }
            //  // 审核6 退回7
        },
        editText() {
            this.isShow = true;
        },
        // 提交
        yj_submit() {

            if ((this.currentRow != null && this.currentRow.fStatus == '监理自评中') || (this.currentRow != null && this.currentRow.fStatus == '已驳回')) {
                if (this.dataFid != "" && this.dataFid != null) {
                    this.$confirm("是否继续提交?", "提示", {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            type: "warning",
                        })
                        .then(() => {
                            this.loading = true;
                            submitCreditEvaluationProcess({
                                    fId: this.dataFid
                                })
                                .then((res) => {
                                    if (res.errcode == 0) {
                                        this.loading = false;
                                        this.dataFid = null
                                        this.isLock = false
                                        this.yj_searchForm(this.getList);
                                        this.$message.success("提交成功");
                                    } else if (res.errcode == -2) {
                                        this.loading = false;
                                        this.$message.warning(res.msg);
                                        this.isLock = false
                                    } else {
                                        this.isLock = false
                                        this.$message.error("提交失败", res.msg);
                                        this.loading = false;
                                        return false;
                                    }
                                })
                                .catch(() => {
                                    this.loading = false;
                                    console.log("提交失败");
                                });
                        })
                        .catch(() => {
                            this.$message("取消提交");
                        });
                } else {
                    return this.$message.error("请先选择需要提交的数据");
                }
            } else if (this.currentRow != null && this.currentRow.fStatus == '业主审核中') {

                if (this.dataFid != "" && this.dataFid != null) {
                    this.$confirm("是否继续提交?", "提示", {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            type: "warning",
                        })
                        .then(() => {
                            this.loading = true;
                            submitCreditEvaluationProcessByOwner({
                                    fId: this.dataFid
                                })
                                .then((res) => {
                                    if (res.errcode == 0) {
                                        this.loading = false;
                                        this.dataFid = null

                                        this.yj_searchForm(this.getList);
                                        this.$message.success("提交成功");
                                    } else if (res.errcode == -2) {
                                        this.loading = false;
                                        this.$message.warning(res.msg);

                                    } else {
                                        this.$message.error("提交失败", res.msg);
                                        this.loading = false;
                                        return false;
                                    }
                                })
                                .catch(() => {
                                    this.loading = false;
                                    console.log("提交失败");
                                });
                        })
                        .catch(() => {
                            this.$message("取消提交");
                        });
                } else {
                    return this.$message.error("请先选择需要提交的数据");
                }
            } else if (this.currentRow != null && this.currentRow.fStatus == '集团审批中') {

                if (this.dataFid != "" && this.dataFid != null) {
                    this.$confirm("是否继续提交?", "提示", {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            type: "warning",
                        })
                        .then(() => {
                            this.loading = true;
                            submitCreditEvaluationProcessByGroup({
                                    fId: this.dataFid
                                })
                                .then((res) => {
                                    if (res.errcode == 0) {
                                        this.loading = false;
                                        this.dataFid = null

                                        this.yj_searchForm(this.getList);
                                        this.$message.success("提交成功");
                                    } else if (res.errcode == -2) {
                                        this.loading = false;
                                        this.$message.warning(res.msg);

                                    } else {
                                        this.$message.error("提交失败", res.msg);
                                        this.loading = false;
                                        return false;
                                    }
                                })
                                .catch(() => {
                                    this.loading = false;
                                    console.log("提交失败");
                                });
                        })
                        .catch(() => {
                            this.$message("取消提交");
                        });
                } else {
                    return this.$message.error("请先选择需要提交的数据");
                }
            } else {
                return this.$message.error("请先选择需要提交的数据");
            }
        },
        // 删除
        yj_delete() {
            if (this.dataFid != "" && this.dataFid != null) {
                this.$confirm("您是否要永久删除此数据?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                    .then(() => {
                        this.loading = true;
                        this.isLock = true;
                        deleteCreditEvaluationProcess({
                                fId: this.dataFid
                            })
                            .then((res) => {
                                if (res.errcode == 0) {
                                    this.loading = false;
                                    this.dataFid = null
                                    this.isLock = false
                                    this.yj_searchForm(this.getList);
                                    this.$message.success("删除成功");
                                } else {
                                    this.isLock = false
                                    this.$message.error("删除失败", res.msg);
                                    this.loading = false;
                                }
                            })
                            .catch(() => {
                                console.log("删除失败");
                                this.loading = false;
                            });
                    })
                    .catch(() => {
                        this.$message("取消删除");
                    });
            } else {
                return this.$message.error("请先选择需要删除的数据");
            }
        },
        // 查看
        // 点击查看 
        yj_viewForm(row, label) {
            this.currentRow = row
            if (label == "信息状态") {
                this.statusNum = 7
                this.isShow = true;
                if (row.fStatus == '业主审核中') {
                    this.status = [true, false, false]
                } else if (row.fStatus == '集团审批中') {
                    this.status = [true, true, false]
                } else if (row.fStatus == '监理自评中') {
                    this.status = [true, false, false]
                } else if (row.fStatus == '已驳回') {
                    this.status = [true, false, false]
                } else if (row.fStatus == '已完成') {
                    this.status = [true, true, true]
                }
            } else if (label == '监理自评') {
                // this.isShow1 = true;
                if (this.userInfo.companyNature == '总监办'||(this.userInfo.companyNature == '集团公司'&&this.getList.modelType=="1")) {
                    this.isShow1 = true;
                } else if (this.userInfo.companyNature == '建设单位') {
                    this.$message.warning("暂无权限");
                }
            } else if (label == '业主评价') {
                if (this.userInfo.companyNature == '总监办') {
                    this.$message.warning("暂无权限");
                } else if (this.userInfo.companyNature == '建设单位'||(this.userInfo.companyNature == '集团公司'&&this.getList.modelType=="1")) {
                    this.isShow1 = true;
                }
            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
            console.log(val);
        },
        // 获取列表数据
        yj_searchForm(data) {
            this.loading = true;
            var that = this;
            var obj = {
                userId: this.userInfo.userId, //当前登陆用户ID
                companyId: data.fCompanyid, //当前登陆用户名称
                modelType: data.modelType, //查询类型 0-待办 1-已办 2-列表页
                keyWord: "", //评价年份
                page: data.page,
                pageSize: data.pageSize
            }
            getCreditEvaluationProcessLists(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },

        handleSelect(key) {
            this.activeIndex = key;
        },
    },
};